package com.faithbj.service.statistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.faithbj.common.model.CheckTotal;
import com.faithbj.common.model.Region;
import com.faithbj.common.model.compare.CompareRecord;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.model.conditions.CheckFamilyStatResult;
import com.faithbj.model.conditions.CompareStatData;
import com.faithbj.model.conditions.CompareStatResult;
import com.faithbj.model.conditions.StreetCompareStatData;
import com.faithbj.model.conditions.StreetFamilyStatData;
import com.faithbj.service.CheckTotalService;
import com.faithbj.service.CompareRecordService;
import com.faithbj.service.FamilyMemberService;

/**
 * 核对业务统计业务逻辑
 * @author m
 *
 */
@Service
public class CheckBusinessStatService {

	@Resource
	private CheckTotalService checkTotalService;
	@Resource 
	private FamilyMemberService familyMemberService;
	@Resource
	private CompareRecordService compareRecordService;
	
	/**
	 * 查询核对家庭统计数据
	 * @param startTime
	 * @param endTime
	 * @param countryRegionList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly=true)
	public List<CheckFamilyStatResult> getCheckFamilyResult(Date startTime, Date endTime, List<Region> countryRegionList) {
		List<CheckFamilyStatResult> resultList = new ArrayList<CheckFamilyStatResult>();
		for(Region countryRegion : countryRegionList) {
//			if(countryRegion.getChildren() == null){
//				continue;
//			}
			if(countryRegion.getChildren() == null || countryRegion.getChildren().size() == 0){
				continue;
			}
			List<StreetFamilyStatData> familyStatDataList = new ArrayList<StreetFamilyStatData>();
			for(Region streetRegion : countryRegion.getChildren()) {
				@SuppressWarnings("rawtypes")
				Map condition = new HashMap();
				condition.put("acceptStartTime", startTime);
				condition.put("acceptEndTime", endTime);
				condition.put("regionId", streetRegion.getId());
				List<CheckTotal> checkInfoList = checkTotalService.queryCheckTotalListByCondition(condition);
				
				Long personNum = new Long("0");
				if(checkInfoList != null && checkInfoList.size() != 0) {
					List<String> applyIdList = new ArrayList<String>();
					for(CheckTotal checkTotal : checkInfoList) {
						applyIdList.add(checkTotal.getId());
					}
					personNum = familyMemberService.getMemberNumOfFamily(applyIdList.toArray(new String[applyIdList.size()]));
				}
				
				StreetFamilyStatData streetFamilyStatData = new StreetFamilyStatData();
				streetFamilyStatData.setStreetName(streetRegion.getName());
				streetFamilyStatData.setFamilyNum(checkInfoList.size());
				streetFamilyStatData.setPersonNum(personNum);
				
				familyStatDataList.add(streetFamilyStatData);
			}
			
			CheckFamilyStatResult result = new CheckFamilyStatResult();
			result.setCountryRegionName(countryRegion.getName());
			result.setStreetFamilyStatDataList(familyStatDataList);
			
			resultList.add(result);
		}
		
		return resultList;
	}
	
	public List<CompareStatResult> getCompareStatResult(Date startTime, Date endTime, List<Region> countryRegionList, String rangeType) {
		List<CompareStatResult> resultList = new ArrayList<CompareStatResult>();
	
		for(Region countryRegion : countryRegionList) {
			if(countryRegion.getChildren() == null || countryRegion.getChildren().size() == 0){
				continue;
			}
			List<StreetCompareStatData>  streetCompareStatDataList = new ArrayList<StreetCompareStatData>();
			for(Region streetRegion : countryRegion.getChildren()) {
				StreetCompareStatData streetCompareStatData = new StreetCompareStatData();
				streetCompareStatData.setStreetRegionName(streetRegion.getName());
				List<CompareStatData> compareStatDataList = new ArrayList<CompareStatData>();
				streetCompareStatData.setCompareStatDataList(compareStatDataList);
				
				Map condition = new HashMap();
				condition.put("acceptStartTime", startTime);
				condition.put("acceptEndTime", endTime);
				condition.put("regionId", streetRegion.getId());
				List<CompareRecord> compareRecordList = compareRecordService.queryCompareRecordListByCondition(condition);
				if(rangeType.equals("0")) {
					CompareStatData dataOfFamily = computDataOfFamily(compareRecordList);
					CompareStatData dataOfMember = computDataOfMember(compareRecordList);
					compareStatDataList.add(dataOfFamily);
					compareStatDataList.add(dataOfMember);
				} else if(rangeType.equals("1")) {
					CompareStatData dataOfFamily = computDataOfFamily(compareRecordList);
					compareStatDataList.add(dataOfFamily);
				} else if(rangeType.equals("2")) {
					CompareStatData dataOfMember = computDataOfMember(compareRecordList);
					compareStatDataList.add(dataOfMember);
				}
				
				streetCompareStatDataList.add(streetCompareStatData);
			}
			CompareStatResult result = new CompareStatResult();
			result.setCountryRegionName(countryRegion.getName());
			result.setStreetCompareStatdataList(streetCompareStatDataList);
			resultList.add(result);
		}
		
		return resultList;
	}

	private CompareStatData add(CompareStatData sum, CompareStatData data) {
		sum.setDishuiRecvNum(sum.getDishuiRecvNum() + data.getDishuiRecvNum());
		sum.setDishuiSendNum(sum.getDishuiSendNum() + data.getDishuiSendNum());
		sum.setFangChanRecvNum(sum.getFangChanRecvNum() + data.getFangChanRecvNum());
		sum.setFangChanSendNum(sum.getFangChanSendNum() + data.getFangChanSendNum());
		sum.setGonganRecvNum(sum.getGonganRecvNum() + data.getGonganRecvNum());
		sum.setGonganSendNum(sum.getGonganSendNum() + data.getGonganSendNum());
		sum.setGongjijinRecvNum(sum.getGongjijinRecvNum() + data.getGongjijinRecvNum());
		sum.setGongjijinSendNum(sum.getGongjijinSendNum() + data.getGongjijinSendNum());
		sum.setGongshangRecvNum(sum.getGongshangRecvNum() + data.getGongshangRecvNum());
		sum.setGongshangSendNum(sum.getGongshangSendNum() + data.getGongshangSendNum());
		sum.setRensheRecvNum(sum.getRensheRecvNum() + data.getRensheRecvNum());
		sum.setRensheSendNum(sum.getRensheSendNum() + data.getRensheSendNum());
		sum.setSendNumSum(sum.getSendNumSum() + data.getSendNumSum());
		sum.setRecvNumSum(sum.getRecvNumSum() + data.getRecvNumSum());
		return sum;
	}
	
	private CompareStatData computDataOfMember(
			List<CompareRecord> compareRecordList) {
		CompareStatData dataOfMember = new CompareStatData();
		dataOfMember.setType(2);
		for(CompareRecord record : compareRecordList) {
			//部委类型：1.房产委2社会保险委3税务委4人口管理机构委办5工商行政管理机构6公积金管理机构
			if(record.getType().equals(1)) {
				dataOfMember.setFangChanSendNum(dataOfMember.getFangChanSendNum() + record.getSendNum());
				dataOfMember.setFangChanRecvNum(dataOfMember.getFangChanRecvNum() + record.getRecvNum());
			} else if(record.getType().equals(2)) {
				dataOfMember.setRensheSendNum(dataOfMember.getRensheSendNum() + record.getSendNum());
				dataOfMember.setRensheRecvNum(dataOfMember.getRensheRecvNum() + record.getRecvNum());
			} else if(record.getType().equals(3)) {
				dataOfMember.setDishuiSendNum(dataOfMember.getDishuiSendNum() + record.getSendNum());
				dataOfMember.setDishuiRecvNum(dataOfMember.getDishuiRecvNum() + record.getRecvNum());
			} else if(record.getType().equals(4)) {
				dataOfMember.setGonganSendNum(dataOfMember.getGonganSendNum() + record.getSendNum());
				dataOfMember.setGonganRecvNum(dataOfMember.getGonganRecvNum() + record.getRecvNum());
			} else if(record.getType().equals(5)) {
				dataOfMember.setGongshangSendNum(dataOfMember.getGongshangSendNum() + record.getSendNum());
				dataOfMember.setGongshangRecvNum(dataOfMember.getGongshangRecvNum() + record.getRecvNum());
			} else if(record.getType().equals(6)) {
				dataOfMember.setGongjijinSendNum(dataOfMember.getGongjijinSendNum() + record.getSendNum());
				dataOfMember.setGongjijinRecvNum(dataOfMember.getGongjijinRecvNum() + record.getRecvNum());
			}
		}
		dataOfMember.setSendNumSum(dataOfMember.getDishuiSendNum() + dataOfMember.getFangChanSendNum() + dataOfMember.getGonganSendNum() +
				dataOfMember.getGongjijinSendNum() + dataOfMember.getGongshangSendNum() + dataOfMember.getRensheSendNum());
		dataOfMember.setRecvNumSum(dataOfMember.getDishuiRecvNum() + dataOfMember.getFangChanRecvNum() + dataOfMember.getGonganRecvNum() +
				dataOfMember.getGongjijinRecvNum() + dataOfMember.getGongshangRecvNum() + dataOfMember.getRensheRecvNum());
		return dataOfMember;
	}

	private CompareStatData computDataOfFamily(List<CompareRecord> compareRecordList) {
		CompareStatData dataOfFamily = new CompareStatData();
		dataOfFamily.setType(1);
		for(CompareRecord record : compareRecordList) {
			//部委类型：1.房产委2社会保险委3税务委4人口管理机构委办5工商行政管理机构6公积金管理机构
			if(record.getType().equals(1)) {
				if(record.getMemberNum().equals(record.getSendNum())) {
					dataOfFamily.setFangChanSendNum(dataOfFamily.getFangChanSendNum() +1);
				}
				if(record.getMemberNum().equals(record.getRecvNum())) {
					dataOfFamily.setFangChanRecvNum(dataOfFamily.getFangChanRecvNum() + 1);
				}
			} else if(record.getType().equals(2)) {
				if(record.getMemberNum().equals(record.getSendNum())) {
					dataOfFamily.setRensheSendNum(dataOfFamily.getRensheSendNum() +1);
				}
				if(record.getMemberNum().equals(record.getRecvNum())) {
					dataOfFamily.setRensheRecvNum(dataOfFamily.getRensheRecvNum() + 1);
				}
			} else if(record.getType().equals(3)) {
				if(record.getMemberNum().equals(record.getSendNum())) {
					dataOfFamily.setDishuiSendNum(dataOfFamily.getDishuiSendNum() +1);
				}
				if(record.getMemberNum().equals(record.getRecvNum())) {
					dataOfFamily.setDishuiRecvNum(dataOfFamily.getDishuiRecvNum() + 1);
				}
			} else if(record.getType().equals(4)) {
				if(record.getMemberNum().equals(record.getSendNum())) {
					dataOfFamily.setGonganSendNum(dataOfFamily.getGonganSendNum() +1);
				}
				if(record.getMemberNum().equals(record.getRecvNum())) {
					dataOfFamily.setGonganRecvNum(dataOfFamily.getGonganRecvNum() + 1);
				}
			} else if(record.getType().equals(5)) {
				if(record.getMemberNum().equals(record.getSendNum())) {
					dataOfFamily.setGongshangSendNum(dataOfFamily.getGongshangSendNum() +1);
				}
				if(record.getMemberNum().equals(record.getRecvNum())) {
					dataOfFamily.setGongshangRecvNum(dataOfFamily.getGongshangRecvNum() + 1);
				}
			} else if(record.getType().equals(6)) {
				if(record.getMemberNum().equals(record.getSendNum())) {
					dataOfFamily.setGongjijinSendNum(dataOfFamily.getGongjijinSendNum() +1);
				}
				if(record.getMemberNum().equals(record.getRecvNum())) {
					dataOfFamily.setGongjijinRecvNum(dataOfFamily.getGongjijinRecvNum() + 1);
				}
			}
		}
		dataOfFamily.setSendNumSum(dataOfFamily.getDishuiSendNum() + dataOfFamily.getFangChanSendNum() + dataOfFamily.getGonganSendNum() +
				dataOfFamily.getGongjijinSendNum() + dataOfFamily.getGongshangSendNum() + dataOfFamily.getRensheSendNum());
		dataOfFamily.setRecvNumSum(dataOfFamily.getDishuiRecvNum() + dataOfFamily.getFangChanRecvNum() + dataOfFamily.getGonganRecvNum() +
				dataOfFamily.getGongjijinRecvNum() + dataOfFamily.getGongshangRecvNum() + dataOfFamily.getRensheRecvNum());
		return dataOfFamily;
	}
}
